package org.marsdonne.commons.jpa;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;

@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper=false)
public class NumberFieldFilter extends BasedFieldFilter {
    private Number value;

    public NumberFieldFilter(Operator operator, Number value) {
        this.operator = operator;
        this.value = value;
    }

    @Override
    public Predicate generateCriteria(CriteriaBuilder builder, Path field) {
        switch (operator) {
            case eq:
                return builder.equal(field, value);
            case lt:
                return builder.lt(field, value);
            case le:
                return builder.le(field, value);
            case gt:
                return builder.gt(field, value);
            case ge:
                return builder.ge(field, value);
            case ne:
                return builder.notEqual(field, value);
            default:
                return null;
        }
    }
}
